<!-- 地区对照表编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'委托经办人维护':'委托经办人维护'"
    :visible="visible"
    width="460px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @open="init"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px">
      <el-form-item
        label="委托经办人1:"
        prop="consignor1">
        <el-input
          :maxlength="20"
          v-model="form.consignor1"
          placeholder=""
          clearable/>
      </el-form-item>
      <el-form-item
        label="委托经办人2:"
        prop="consignor2">
        <el-input
          :maxlength="150"
          v-model="form.consignor2"
          placeholder=""
          clearable/>
      </el-form-item>
      <el-form-item
        label="记录人:"
        prop="operator2">
        <el-select
          clearable
          v-model="form.operator2"
          size="small"
          placeholder="-请选择车牌号-"
          class="ele-block">
          <el-option v-for="item in operList" :key="item.id" :label="item.operName" :value="item.operName"/>
        </el-select>

      </el-form-item>
      <el-form-item
        label="车牌号:"
        prop="plate_no">
        <el-select
          clearable
          v-model="form.plate_no"
          size="small"
          placeholder="-请选择车牌号-"
          class="ele-block">
          <el-option v-for="item in plateNoList" :key="item.id" :label="item.plateNo" :value="item.plateNo"/>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'SineOperatorEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form:{
        id:0,
        consignor1:'',
        consignor2:'',
        operator2:'',
        plate_no:'',
      },
      regionList:[],
      plateNoList:[],
      operList:[],
      // 表单验证规则
      rules: {
        consignor1: [
          {required: true, message: '请输入委托经办人', trigger: 'blur'}
        ],
        plate_no: [
          {required: true, message: '请输入运输车牌号', trigger: 'blur'}
        ],
        operator2: [
          {required: true, message: '请输入记录人', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form.consignor1 = this.data.consignor1;
        this.form.consignor2 = this.data.consignor2;
        this.form.id = this.data.id;
        this.form.operator2 = this.data.operator2;
        this.form.plate_no = this.data.plateNo;
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    //console.log(this.data);
  },
  methods: {
    init(){
      this.getOperList();
      this.getTruckList();
    },
    async getOperList(){
      var res = await this.$http.get('/kdltrans/operlist?vendor='+this.data.consignee);
      if(res.status==200)
      {
        this.operList = res.data.data;
      }

    },
    async getTruckList(){
      var res = await this.$http.get('/kdltrans/trucklist?vendor='+this.data.consignee);
      if(res.status==200)
      {
        this.plateNoList = res.data.data;
      }

    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http[this.isUpdate ? 'post' : 'post'](this.isUpdate ? '/kdl/savetransoper' : '/kdl/savetransoper', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 10px !important;
}
</style>
